import PyDGGS

# 本example主要是用来展示如何使用DGGSFeature库实现对矢量数据的存储管理和保存

def create_feature(feature_class, feature_id, codes):
    """
    创建一个feature要素类型对象
    :param feature_class: feature_class类对象
    :param feature_id: 要素对象的id属性
    :param codes: 格网编码的集合
    :return: 要素对象
    """
    # 创建新要素
    feature = feature_class.createFeature()

    # 为feature设置fId
    feature.setFID(feature_id)

    # 几何类型
    geo_type = feature_class.getGeometryType()

    grid_count = len(codes)
    # 创建几何对象
    if geo_type == PyDGGS.ST_Point:
        point = PyDGGS.DGGSPoint()
        point.setGridCount(grid_count)
        for code in codes:
            point.addGridCode(code)
        feature.setGeometry(point)
    elif geo_type == PyDGGS.ST_PolyLine:
        line = PyDGGS.DGGSPolyLine()
        line.setGridCount(grid_count)
        for code in codes:
            line.addGridCode(code)
        feature.setGeometry(line)
    elif geo_type == PyDGGS.ST_Polygon:
        polygon = PyDGGS.DGGSPolygon()
        polygon.setGridCount(grid_count)
        for code in codes:
            polygon.addGridCode(code)
        feature.setGeometry(polygon)

    return feature

def delete_feature(feature_layer, feature_id):
    """
    删除图层中的要素
    :param feature_layer: 原图层对象
    :param feature_id: 要素id
    :return: 修改后的图层对象
    """
    feature = feature_layer.getFeature(feature_id)
    feature_layer.eraseFeature(feature)
    return feature_layer

def add_feature(feature_layer, feature_id, codes):
    """
    添加要素
    :param feature_layer: 原要素图层
    :param feature_id: 属性id
    :param codes: 格网要素编码集合
    :return: 添加要素后的图层
    """
    feature_class = feature_layer.getFeatureClass()
    feature = create_feature(feature_class, feature_id, codes)
    feature_layer.addFeature(feature)
    return feature_layer

def update_feature(feature_layer, feature_id, codes):
    """
    更新要素对象
    :param feature_layer: 原图层
    :param feature_id: 更新的要素id
    :param codes: 新的几何属性
    :return: 更新后的图层
    """
    # 删除原要素
    feature = feature_layer.getFeature(feature_id)
    feature_layer.eraseFeature(feature)

    # 添加新要素
    feature_class = feature_layer.getFeatureClass()
    feature = create_feature(feature_class, feature_id, codes)
    feature_layer.addFeature(feature)

    return feature_layer

def find_feature(feature_layer, feature_id):
    """
    根据id查找要素对象
    :param feature_layer: 原要素图层
    :param feature_id: 查找要素id
    :return: 返回查找对象
    """
    feature = feature_layer.getFeature(feature_id)
    return feature


def main():

    # 属性设置
    element_type = PyDGGS.DGGSElementType_Cell
    grid_type = PyDGGS.DGGSGridType_DGGS_ISEA4H
    geo_type = PyDGGS.ST_Polygon
    level = 5

    # 创建feature_class类
    feature_class = PyDGGS.DGGSFeatureClass(level, geo_type, grid_type, element_type)

    # 创建一个feature的图层类
    feature_layer = PyDGGS.DGGSFeatureLayer(feature_class)

    # 创建feature对象
    # 设置几何
    codes = []
    code = PyDGGS.DGGSElementCode()
    code.elementType = element_type
    code.level = level
    code.basePartition = 1
    code.col = 0
    for row in range(10):
        code.row = row
        codes.append(code)

    feature_id = 0

    # 添加要素
    feature_layer = add_feature(feature_layer, feature_id, codes)
    print(f"添加完成，图层要素数量：{feature_layer.getFeatureCount()};feature的几何对象格网数量为:{feature_layer.getFeature(feature_id).getGeometry().getGridCount()}")

    # 更新要素
    codes.append(code)
    feature_layer = update_feature(feature_layer, feature_id, codes)
    print(f"更新完成，图层要素数量：{feature_layer.getFeatureCount()};feature的几何对象格网数量为:{feature_layer.getFeature(feature_id).getGeometry().getGridCount()}")

    # 查找要素
    feature = find_feature(feature_layer, feature_id)
    print(f"查找完成，图层要素id：{feature.getFID()}")

    # 删除要素
    feature_layer = delete_feature(feature_layer, feature_id)
    print(f"删除完成，图层要素数量：{feature_layer.getFeatureCount()}")


if __name__ == "__main__":
    main()
